package jnpf.engine.custom.type;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import jnpf.base.UserInfo;
import jnpf.engine.custom.cmd.commons.ExpressionCmd;
import jnpf.engine.model.flowengine.shuntjson.childnode.GroupsModel;
import jnpf.engine.model.flowengine.shuntjson.childnode.ProperCond;
import jnpf.engine.model.flowengine.shuntjson.childnode.Properties;
import jnpf.engine.model.flowtask.FlowConditionModel;
import jnpf.engine.util.EvalUtils;
import jnpf.engine.util.FlowContextHolder;
import jnpf.engine.util.FlowTaskUtil;
import jnpf.engine.util.SpringContextHolder;
import jnpf.permission.entity.UserEntity;
import jnpf.permission.service.UserService;
import jnpf.util.JsonUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import jnpf.util.visiual.JnpfKeyConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.ManagementService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.delegate.JavaDelegate;
import org.flowable.engine.runtime.Execution;
import org.springframework.stereotype.Component;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static jnpf.engine.custom.constants.GlobalProcessVariableConstants.APPLY_USER_ID;
import static jnpf.engine.custom.constants.GlobalProcessVariableConstants.CURRENT_CREATE_TIME;
import static jnpf.engine.util.BpmnModelUtils.*;
import static jnpf.engine.util.EvalUtils.*;
import static jnpf.engine.util.EvalUtils.search;

@Component
@Slf4j
public class JumpTaskListener implements ExecutionListener {

  @Override
  public void notify(DelegateExecution execution) {

    UserProvider userProvider = SpringContextHolder.getBean(UserProvider.class);
    UserService userService = SpringContextHolder.getBean(UserService.class);
    ScriptEngine scriptEngine = SpringContextHolder.getBean(ScriptEngine.class);
    RuntimeService runtimeService = SpringContextHolder.getBean(RuntimeService.class);
    FlowTaskUtil flowTaskUtil = SpringContextHolder.getBean(FlowTaskUtil.class);
    ManagementService managementService = SpringContextHolder.getBean(ManagementService.class);
    String currentActivityId = execution.getCurrentActivityId();
    Process bpmnModelMainProcess = getBpmnModelMainProcess(execution.getProcessDefinitionId());
    String manualTaskStr = getAttributeValueByTaskDefinitionKey(bpmnModelMainProcess, currentActivityId, "manualTask");
    Properties properties = JSONObject.parseObject(manualTaskStr, new TypeReference<Properties>() {
    });
    String jumpTaskNode = properties.getJumpTaskNode();

    List<ProperCond> conditions = properties.getConditions();
    String conditionExpStr = assemConditionExpStr("hhhh", properties, conditions,execution.getCurrentActivityId());
    Boolean flag = managementService.executeCommand(new ExpressionCmd(execution.getProcessInstanceId(), conditionExpStr, new HashMap<>()));
    if(flag){

      runtimeService.createChangeActivityStateBuilder()
              .processInstanceId(execution.getProcessInstanceId())
              .moveActivityIdTo(execution.getCurrentActivityId(),jumpTaskNode)
              .changeState();
    }
    else{
      log.error("路由跳转节点节点条件不成立,自动往下流转");
    }

  }
  private List<String> getMainExecutions(String processInstanceId) {
    RuntimeService runtimeService = SpringContextHolder.getBean(RuntimeService.class);
    List<Execution> dbExecList = runtimeService.createExecutionQuery().parentId(processInstanceId).list();
    List<String> executionsList=new ArrayList<>();
    for (Execution execution : dbExecList) {
      executionsList.add(execution.getId());
    }
    return executionsList;
  }



}
